var base = require('../../utils/base.js')
var memberId = base.getMemberData().memberId
var memberData = base.getMemberData().memberData
var WxParse = require('../../utils/wxParse/wxParse.js')
var feedbackApi = require('../../utils/showToast.js');//引入消息提醒暴露的接口  
var app = getApp()
var id;

Page({
  data: {
    showPanel: false,
    main: {},
    content: '',
    placeholder: '请输入评论内容',
    memberId: memberId,
    video_play_mark: true,
    pic_play_mark: true,
    login:false,
    comment:{},
    danmuList:[]
  },
  onLoad: function (options) {
    memberId = base.getMemberData().memberId
    
    // 页面初始化 options为页面跳转所带来的参数
    id = options.id
    var that = this
    // that.auth()
    that.loadData()

  },
  auth(){
    var that = this
    app.getUserInfo(function (userInfo, status) {
      if (status) {//授权成功
        memberId = base.getMemberData().memberId
        that.setData({
          login: true,
          member_id: memberId
        })
        console.log('授权成功')
      } else {//授权失败
        console.log('授权失败')
        that.openSetting()
      }
    }, false)
  },
  loadData() {
    var that = this
    memberId = base.getMemberData().memberId
    that.setData({
      member_id: memberId
    })
    base.getJson('Article/articleDetail', { id: id }, function (data) {
      // console.log(data)
      wx.setNavigationBarTitle({
        title: data.article.title
      })
      var arr = []
      for (var i = 0; i < data.article.comment.length;i++){
        var aa = { text: data.article.comment[i].content, color: that.getRandomColor(), time: Math.floor(Math.random() * 10)}
        arr.push(aa)
      }
      // console.log(arr)
      data.article.article_detail.content == null ? data.article.article_detail.content='' : data.article.article_detail.content
      WxParse.wxParse('article', 'html', data.article.article_detail.content, that);
      
      that.setData({
        main: data.article.article_detail,
        comment: data.article.comment,
        danmuList:arr
      })

    })



  },
  getRandomColor:function () {
    let rgb = []
  for (let i = 0 ; i < 3; ++i){
  let color = Math.floor(Math.random() * 256).toString(16)
  color = color.length == 1 ? '0' + color : color
  rgb.push(color)
}
return '#' + rgb.join('')
},
  //设置分享
  onShareAppMessage: function () {
    return {
      title: '「舞蹈视频」 ' + this.data.main.title,
      desc: '每日国内外最新舞蹈视频',
      path: '/pages/video/videoDetail?id=' + id
    }
  },
  onReady: function () {
    // 页面渲染完成
    this.videoContext = wx.createVideoContext('myVideo')
  },
  bindContentChange: function (e) {
    // console.log(e.detail.value)
    this.setData({
      content: e.detail.value
    })
    // if (e.detail.value != '' && e.detail.value != null && e.detail.value) {
    //   this.setData({
    //     showPanel: true
    //   })
    // } else {
    //   this.setData({
    //     showPanel: false
    //   })
    // }
  },
  openSetting: function () {
    var that = this
    wx.openSetting({
      success: (res) => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          memberId = base.getMemberData().memberId
        }
      }
    })
  },
  hidePanel: function () {
    this.setData({
      showPanel: false
    })
  },
  showPanel: function () {

    this.setData({
      showPanel: true,
    })
  },
  trim: function (str) {
    return str.replace(/(^\s*)|(\s*$)/g, '');
  },
  sendComment: function () {
    var that = this;
    memberId = base.getMemberData().memberId
 
    if (memberId) {
      var content = that.data.content
      if (that.trim(content) == '') {
        feedbackApi.showToast({
          title: '请输入内容',
          icon: '../../image/warning.png',
          duration: 2500
        })
        return;
      }else{
        base.getJson('Common/comment', { source_id: id, source_type: "article", content: content }, function (data) {

          that.loadData()
          that.setData({
            content: ''
          })
          that.hidePanel()
        })
      }
    }else{
      that.auth()
    }
  },
  delComment: function (e) {
    var that = this
    var comment_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '删除后不可恢复，确定删除吗？',
      confirmColor: '#00cfc7',
      success: function (res) {
        if (res.confirm) {
          base.getJson('Common/commentDelete', { id: comment_id}, function (data) {

            that.loadData()
            that.setData({
              content: ''
            })
            that.hidePanel()
          })
        }

      }
    })
  },
  togglePlay: function () {
    var video_play_mark = this.data.video_play_mark;
    if (video_play_mark) {
      this.videoContext.play();
    } else {
      this.videoContext.pause();
    }
  },
  play: function (e) {
    this.setData({
      video_play_mark: false,
      pic_play_mark: false
    })
  },
  pause: function (e) {
    this.setData({
      video_play_mark: true
    })
  },
  reply: function (e) {
    var that = this
    var comment_id = e.currentTarget.dataset.id
  },
  // 播放结束弹出框
  playEnd:function(){
    wx.showModal({
      title: '感谢观看',
      content: '更多视频请下载街舞酱APP，点击"确定"进入APP下载二维码页面',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.navigateTo({
            url: '../teach/classDetail',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})